package com.lkd.job;


import com.lkd.common.VMSystem;
import com.lkd.entity.UserEntity;
import com.lkd.service.UserService;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.time.Duration;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;


@Component
public class UserJob {

    @Autowired
    private UserService userService;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;


    /**
     * 工单列表初始化
     * @param param
     * @return
     */
    @XxlJob("workCountInitJobHandler")
    public ReturnT<String> workCountInitJobHandler(String param) {
        // 查询用户列表
        int plusDays = "0".equals(param) ? 0 : 1;
        userService.list().forEach(userEntity -> {
            if (userEntity.getRoleId().intValue() != 0) // 判断是否是管理员
            {
                String key = VMSystem.REGION_TASK_KEY_PREF
                        + LocalDate.now().plusDays(plusDays).format(DateTimeFormatter.ofPattern("yyyyMMdd"))
                        + "." + userEntity.getRegionId() + "." + userEntity.getRoleCode();
                redisTemplate.opsForZSet().add(key ,userEntity.getId() ,0);
                redisTemplate.expire(key, Duration.ofDays(2));
            }
        });
        return ReturnT.SUCCESS;
    }
}
